<template>
  <div>
    <el-tabs v-model="activeKey" @tab-click="handleTab">
      <el-tab-pane label="企业付款" name="1"> </el-tab-pane>
      <el-tab-pane label="企业批量付款" name="2"></el-tab-pane>
    </el-tabs>
    <el-card style="min-width: 1200px">
      <div class="tableSelectWrap">
        <el-form ref="qiyeForm" :model="commandForm" label-width="100px">
          <el-row>
            <el-col :span="7">
              <el-form-item prop="date" label="选择时间">
                <el-date-picker
                  v-model="commandForm.date"
                  style="
                    width: 100%;
                    border-radius: 0 4px 4px 0;
                    margin-top: 1px;
                  "
                  type="datetimerange"
                  start-placeholder="开始时间"
                  :default-time="['00:00:00', '23:59:59']"
                  end-placeholder="结束时间"
                  :clearable="false"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1" v-if="activeKey == '1'">
              <el-form-item label="付款状态" prop="status">
                <el-select
                  v-model="commandForm.status"
                  style="width: 100%"
                  placeholder="请选择付款状态"
                  clearable
                >
                  <el-option
                    v-for="item of payStateList"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1">
              <el-form-item label="付款渠道" prop="payformChannel">
                <el-select
                  v-model="commandForm.payformChannel"
                  style="width: 100%"
                  placeholder="请选择付款渠道"
                  clearable
                >
                  <el-option
                    v-for="(item, index) of payChannel"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col
              v-if="activeKey == '2'"
              :span="7"
              :offset="1"
              style="min-width: 170px"
            >
              <el-button class="width120" @click="this.resetFields"
                >重置</el-button
              >
              <el-button
                type="primary"
                class="width120"
                @click="()=>{
               this.dataForm=JSON.parse(JSON.stringify(this.commandForm));
               this.handleQuery()
              }"
                >查询</el-button
              >
            </el-col>
          </el-row>
          <el-row>
            <el-col v-if="activeKey == '1'" :span="7" style="min-width: 170px">
              <el-button class="width120" @click="this.resetFields"
                >重置</el-button
              >
              <el-button
                type="primary"
                class="width120"
                @click="()=>{
               this.dataForm=JSON.parse(JSON.stringify(this.commandForm));
               this.handleQuery()
              }"
                >查询</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card style="margin-top: 15px; min-width: 1200px">
      <div class="switch">
        <div>
          <el-switch v-model="isFixedTableH" inactive-text="固定表头">
          </el-switch>
        </div>
        <div>
          <el-button type="primary" v-if="activeKey === '2'" @click="createPayAll">创建批量付款</el-button>
        </div>
      </div>
      <el-table :height="fixedTableHeight" :data="dataList" :key="activeKey">
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '1'"
          prop="transferNo"
          align="center"
          label="商户订单号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.transferNo ? scope.row.transferNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '2'"
          prop="merBatchNo"
          align="center"
          label="批次号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.merBatchNo ? scope.row.merBatchNo : "-" }}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '1'"
          prop="fmtTransAmt"
          align="center"
          label="付款金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtTransAmt ? scope.row.fmtTransAmt : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '2'"
          prop="fmtTotalAmount"
          align="center"
          label="付款总金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtTotalAmount ? scope.row.fmtTotalAmount : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '2'"
          prop="totalNum"
          align="center"
          label="付款总笔数/笔"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.totalNum ? scope.row.totalNum : "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="channelName"
          align="center"
          label="付款渠道"
        >
          <template slot-scope="scope">
            <span
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              ">{{
             scope.row.channelName ? scope.row.channelName: "-"
              }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '1'"
          prop="statusName"
          align="center"
          label="付款状态"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
            {{scope.row.statusName?scope.row.statusName:''}}

            </div>
          </template>
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          v-if="activeKey === '2'"
          align="center"
          label="结果"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{'成功'+scope.row.successCount+'失败'+scope.row.failCount}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="act" width="200" align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetails(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      layout="prev, pager, next, sizes, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :current-page="pageNum"
      :total="total"
      @current-change="pageCurrentChangeHandle"
      @size-change="pageSizeChangeHandle"
    >
    </el-pagination>
    <!-- 详情抽屉 -->
    <detail-drawer
      :drawer.sync="drawer"
      :info="detailsInfo"
      :activeKey="activeKey"
    ></detail-drawer>
  </div>
</template>

<script>
import detailDrawer from "./detailDrawer.vue";
import { setTabIndex, getTabIndex } from "@/utils/public";
import mixin from "@/mixins/mixin";
export default {
  mixins: [mixin],
  components: {
    detailDrawer,
  },
  name: "qiyePay",
  data() {
    return {
      activeKey: getTabIndex(),
      mixinViewModuleOptions: {
        getDataListURL: "/api/transfer/listByPage",
        createdIsNeed: false,
      },
      dataList: [],
      detailsInfo: {},
      drawer: false,
      payStateList: [
        // 付款状态
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "处理中", value: "processing" },
        { id: 3, label: "处理成功", value: "success" },
        { id: 4, label: "处理失败", value: "fail" },
      ],
      payChannel: [
        // 付款渠道
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "微信APP", value: "wx_app" },
        { id: 3, label: "微信公众号", value: "wx_pub" },
        { id: 4, label: "微信小程序", value: "wx_lite" },
        { id: 5, label: "支付宝", value: "ali_transfer" },
        { id: 6, label: "汇付天下", value: "ada" },
      ],
      commandForm: {
        payformChannel: "",
        date: [
          this.$moment().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
          this.$moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
        ],
        gmtCreatedBegin: this.$moment()
          .startOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        gmtCreatedEnd: this.$moment()
          .endOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
          status: "",
      },
    };
  },
  created() {
  },
  mounted() {
    this.handleTab();
  },
  watch: {
    "$store.state.testLine": {
      handler() {
        if (this.$route.name == this.$options.name) {
          this.handleQuery();
        }
      },
    },
    deep: true,
  },
  methods: {
    getTabIndex,
    //重置操作
    resetFields() {
      this.resetForm("qiyeForm");
    },
    handleDetails(rowData) {
      switch (this.activeKey) {
        case "1":
          this.$http
            .get(`/api/transfer/detail?id=${rowData.id}`)
            .then((res) => {
              if (res.code === "0000") {
                this.detailsInfo = res.data;
                this.drawer = true;
              }
            });
          break;
        case "2":
          this.$http
            .get(
              `/api/transfer/batch/detail?id=${rowData.id}`
            )
            .then((res) => {
              if (res.code === "0000") {
                this.detailsInfo = res.data;
                this.drawer = true;
              }
            });
          break;
        default:
          break;
      }
    },
    handleTab() {
      this.dataList=[]
      setTabIndex(this.activeKey);
      this.pageNum = 1;
      this.pageSize = 10;
      this.resetForm("qiyeForm");
    this.dataForm=JSON.parse(JSON.stringify(this.commandForm))
      this.handleQuery();
    },
    setAuditStatus(val) {
      let color = "";
      let str = "";
      switch (val) {
        case "processing":
          str = "处理中";
          color = "pedding";
          break;
        case "success":
          str = "处理成功";
          color = "succeed";
          break;
        case "fail":
          str = "处理失败";
          color = "failed";
          break;
        // case "1":
        //   str = "待发送";
        //   color = "pedding";
        //   break;
        // case "2":
        //   str = "已取消";
        //   color = "";
        //   break;
        // case "3":
        //   str = "已退票";
        //   color = "";
        //   break;
      }
      return {
        str,
        color,
      };
    },

    handleQuery() {
      if (this.dataForm.date && this.dataForm.date.length) {
        this.dataForm.gmtCreatedBegin = this.dataForm.date[0];
        this.dataForm.gmtCreatedEnd = this.dataForm.date[1];
      } else {
        this.dataForm.gmtCreatedBegin = "";
        this.dataForm.gmtCreatedEnd = "";
      }
      this.getData();
    },
    getData() {
      if (this.activeKey == "1") {
        this.mixinViewModuleOptions.getDataListURL =
          "/api/transfer/listByPage";
      } else {
        this.mixinViewModuleOptions.getDataListURL =
          "/api/transfer/batch/listByPage";
      }
      this.getDataList();
    },

    createPayAll(){
this.$router.push('/deal/transfer_batch')
    },
  },
};
</script>

<style scoped lang="scss">
.pedding {
  color: #f69c1f;
}
.succeed {
  color: #07a555;
}
.failed {
  color: #f42b2b;
}
.switch {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
